package com.siteseat.crudeq.broker.security;

import com.siteseat.common.foundation.PropertyHelper;
import com.siteseat.common.security.SecureComponent;
import com.siteseat.common.security.SecurityException;
import com.siteseat.crudeq.kernel.KernelFactory;
import com.siteseat.crudeq.kernel.Queue;
import com.siteseat.crudeq.kernel.QueueListenerManager;
import com.siteseat.crudeq.kernel.QueueManager;
import com.siteseat.crudeq.kernel.QueueProfile;
import com.siteseat.crudeq.kernel.exception.DeployException;

/**
 *
 */
public class SecureQueueManager extends SecureComponent implements QueueManager {

  private QueueManager impl;

  /**
   *
   *
   * @param impl
   */
  public SecureQueueManager( QueueManager impl ) {
    this.impl = impl;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public KernelFactory getFactory() {
    return impl.getFactory();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void deploy( String actor, QueueProfile profile ) throws DeployException {
    try {
      ensureGranted( actor, null, BrokerPermission.QUEUE_DEPLOY );
    } catch( SecurityException e ) {
      throw new DeployException(e);
    }
    impl.deploy( actor, profile );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Queue ensureDeployed( Integer qid ) throws DeployException {
    return impl.ensureDeployed( qid );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Queue ensureDeployed( String name ) throws DeployException {
    return impl.ensureDeployed( name );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void ensureNotDeployed( Integer qid ) throws DeployException {
    impl.ensureNotDeployed( qid );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void ensureNotDeployed( String name ) throws DeployException {
    impl.ensureNotDeployed( name );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Queue find( Integer qid ) {
    return impl.find( qid );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Queue find( String queueName ) {
    return impl.find( queueName );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isDeployed( Integer qid ) {
    return impl.isDeployed( qid );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isDeployed( String name ) {
    return impl.isDeployed( name );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void undeploy( String actor, Integer qid ) throws DeployException {
    try {
      ensureGranted( actor, null, BrokerPermission.QUEUE_UNDEPLOY );
    } catch( SecurityException e ) {
      throw new DeployException(e);
    }
    impl.undeploy( actor, qid );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void undeploy( String actor, String name ) throws DeployException {
    try {
      ensureGranted( actor, null, BrokerPermission.QUEUE_UNDEPLOY );
    } catch( SecurityException e ) {
      throw new DeployException(e);
    }
    impl.undeploy( actor, name );
  }

  /**
   * {@inheritDoc}
   */
  public QueueListenerManager getListenerManager() {
    return impl.getListenerManager();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void doInit() throws Exception {
    super.doInit();
    
    PropertyHelper.ensureInitialized( log, this.impl, "impl" );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void doDestroy() throws Exception {
    // do nothing so far
  }
  
  
  
}
